package lab10;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;


public class RequestWorker implements Runnable
{
	private InputStreamReader in;
	private OutputStream out;
	private HttpServer server;
	private String hi = "Doug Sweeney was here! Hi, from Cedar Rapids Iowa.\r\n";
	private String na = "HTTP/1.0 404 Not Found";
	private String ok = "HTTP/1.0 200 OK";
	private String contentLength = "Content-length: ";
	private String contentType = "Content-type: ";
	private String textType = "text/html";
	private String icoType = "image/x-icon";
	private String crlf = "\r\n";
	private Socket socket;
	
	public RequestWorker(Socket socket, HttpServer server) throws IOException
	{
		in = new InputStreamReader(socket.getInputStream());
		out = socket.getOutputStream();
		this.server = server;
		this.socket = socket;
	}
	
	private void processRequest (String requests) throws IOException
	{
		String[] elements = requests.split("[\" \"]");
		
		for (int i = 0; i < elements.length; i++)
		{
			if(elements[i].contentEquals("GET"))
			{
				this.processGet(elements[i + 1]);
			}
			else
			{
				//not processing any other requests at this time
			}
		}

	}
	
	private void processGet (String resource) throws IOException
	{
		if(resource.contains("favicon"))
		{
			sendFavicon();
		}
		else if(resource.contains("/"))
		{
			String[] stringArr = resource.split("[/]");
			
			if(stringArr.length == 0)
			{
				System.out.println("index");
				File file = new File("./src/index.html");
				byte[] fileBytes = server.readFile(file);
				
				out.write((na+crlf).getBytes());
				out.write((contentLength+fileBytes.length+crlf).getBytes());
				out.write((contentType+textType+crlf+crlf).getBytes());
				out.write(fileBytes);
				out.flush();
			}
			else
			{
				System.out.println(resource);
				File file = new File("./src"+resource);
				if(file.exists())
				{
					byte[] fileBytes = server.readFile(file);

					out.write((na+crlf).getBytes());
					out.write((contentLength+fileBytes.length+crlf).getBytes());
					out.write((contentType+textType+crlf+crlf).getBytes());
					out.write(fileBytes);
					out.flush();
				}
				else
				{
					sendNotFound();	
				}
			}
		}
		else
		{
			sendNotFound();	
		}
		
	}
	
	private void sendFavicon () throws IOException
	{
		out.write((ok+crlf).getBytes());
		out.write((contentLength+server.getFavicon().length+crlf).getBytes());
		out.write((contentType+icoType+crlf+crlf).getBytes());
		out.write(server.getFavicon());
		out.flush();
	}
	
	private void sendNotFound() throws IOException
	{
		System.out.println("not found");
		File file = new File("./src/Not_Found.html");
		byte[] fileBytes = server.readFile(file);
		
		out.write((na+crlf).getBytes());
		out.write((contentLength+fileBytes.length+crlf).getBytes());
		out.write((contentType+textType+crlf+crlf).getBytes());
		out.write(fileBytes);
		out.flush();	
	}
	
	@Override
	public void run()
	{
		try
		{
			BufferedReader reader = new BufferedReader(in);
			String fromServer = new String();
			String line;

			do{
				line = reader.readLine();
				fromServer += line;
				fromServer += " ";
				System.out.println(line);
				
			}while((line != null) && (!line.equals("")));
			
			this.processRequest(fromServer);
						
			socket.close();
			server.connectionReleased();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
